<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<STYLE TYPE="text/css">
		BODY {
			background-color: f5f5f5;
			font-family: arial, verdana; font-size: small;
		}
		H2, H3, A, .tfc {
			color: #000080;
			font-family: arial, verdana; font-size: small;
		}
		PRE { 
		    font-family: monospace;
			border: 1px lightgrey dotted;
		    white-space: pre; 
		    color: black;
		    background-color: #efefef; 
		}
		TABLE {
			float: right;
			border-collapse: collapse;
			border-top: 1px solid #000000;
			border-right: 1px solid #000000;
			border-left: 1px solid #000000;
		}
		TH {
			padding-top: 2px;
			padding-bottom: 2px;
			padding-right: 5px;
			padding-left: 5px;
		}
		TD {
			padding-top: 2px;
			padding-bottom: 2px;
			padding-right: 5px;
			padding-left: 5px;
			border-bottom: 1px solid #000000;
			border-right: 1px solid #000000;
			font-family: arial, verdana; font-size: small;
		}
	</STYLE>
<TITLE>Msgno</TITLE>
</HEAD>
<BODY>
<H1>10. Msgno</H1>
The <I>msgno</I>(3m) module provides a set of macros that when used consistently will generate stacktrace-like output like the following example:
<PRE>

  src/expatls.c:97:utf8tods: Character encoding error
    src/expatls.c:449:start_fn: 
    src/dom.c:405:DOM_Element_normalize: 
    dump.c:30:main: Failed to process sample.xml
  </PRE>
<p>
</p>
<i>Note: As of version 0.9, this implementation no longer uses variadic macros -- it is strict standard C.</i>
<p>
</p>
Additionally this module provides functions for managing error codes (or more generically message numbers) and associated messages across separate C libraries. This functionality is very similar to the <TT>com_err</TT> library but with runtime message registration. Each participating library registers a table of messages at runtime with the <TT>msgno_add_codes</TT> function. The <I>msgno</I>(3m) macros are provided to dispatch messages (e.g. print to <tt>stderr</tt>).
<p>
</p>
<i>Note: The <def>msgno</def>(3m) macros operate on a shared buffer and therefore they are not reentrant. Meaning they cannot not be used concurrently by multiple threads.</i>
<H3>10.1. MMSG, MMNO, MMNF, PMSG, PMNO, PMNF, AMSG, AMNO, AMNF</H3>
The nine <TT>msgno</TT> macros are used to initiate, append, and finally dispatch messages and formatted output to a user defined handler (the default is <tt>stderr</tt>).
<A name="The 9 Msgno Macros"></A>
<P>
<B CLASS="tfc">The 9 Msgno Macros</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/msgno.h&gt;
	
  MMSG(fmt, ...)
  MMNO(msgno)
  MMNF(msgno, fmt, ...)
  
  /* Primary */
  PMSG(fmt, ...)
  PMNO(msgno)
  PMNF(msgno, fmt, ...)
  
  /* Additional */
  AMSG(fmt, ...)
  AMNO(msgno)
  AMNF(msgno, fmt, ...)
  
  extern int (*msgno_hdlr)(const char *fmt, ...);
  
  struct msgno_entry {
  	unsigned int msgno;
  	const char *msg;
  };
  </PRE>
<B>Description</B>
<BR>
The <i>Primary</i> and <i>Additional</i> macros (begin with P and A) do not dispatch messages to the <tt>msgno_hdlr</tt> but instead write to a global buffer. The <tt>MMSG</tt>, <tt>MMNO</tt>, or <tt>MMNF</tt> macros will dispatch messages to the <tt>msgno_hdlr</tt> in addition to any <i>Primary</i> and <i>Additional</i> messages in the global buffer. The general flow should be to use a <i>Primary</i> macro to initiate a new buffered message, then use the <i>Additional</i> macros to append messages, and finally trigger the entire stack-trace-like message to be dispatched to the <tt>msgno_hdlr</tt> with a <tt>MMSG</tt>, <tt>MMNO</tt>, or <tt>MMNF</tt> macro.
<TABLE>
<CAPTION>
<B>Table 1. </B>
</CAPTION>
<tr>
<th></th><th>Formatted String</th><th>Message Number</th><th>Message Number and Formatted String</th>
</tr>
<tr>
<td>Primary message at the beginning of the message buffer</td>
<td><b>PMSG</b> - The <i>primary message</i> macro writes formated <tt>printf</tt> like string to the beginning of the message buffer</td>
<td><b>PMNO</b> - The <i>primary message number</i> macro accepts just a message number and writes the associated message the to the beginning of the message buffer</td>
<td><b>PMNF</b> - The <i>primary message number format</i> macro accepts a message number and a formatted <tt>printf</tt> like string and writes both the message associated with the message number and the formatted output to the beginning of the message buffer</td>
</tr>
<tr>
<td>Additional message appended to the message buffer</td>
<td><b>AMSG</b> - The <i>additional message</i> macro appends a formated <tt>printf</tt> like string to the message buffer</td>
<td><b>AMNO</b> - The <i>additional message number</i> macro accepts just a message number and appends the associated message the to the message buffer</td>
<td><b>AMNF</b> - The <i>additional message number format</i> macro accepts a message number and a formatted <tt>printf</tt> like string and appends both the message associated with the message number and the formatted output to the message buffer</td>
</tr>
<tr>
<td>Dispatched immediatedly to <tt>msgno_hdlr</tt></td>
<td><b>MMSG</b> - The <i>message</i> macro writes a formatted string to the registered <tt>msgno_hdlr</tt></td>
<td><b>MMNO</b> - THe <i>message number</i> macro writes the message associated with the provided number to the <tt>msgno_hdlr</tt></td>
<td><b>MMNF</b> - The <i>message number format</i> macro writes both the message associated with the message number and a formatted <tt>printf</tt> like string to the <tt>msgno_hdlr</tt>.</td>
</tr>
</TABLE>
<p>
</p>
The <TT>msgno</TT> macros are designed to be the least intrusive way to place debugging information within C source code. The following is an example of how and where these macros might be used to generate the example stack-trace-like output listed above.
<PRE>

  if ((n = dec_mbsncpy(&amp;s, sn, NULL, -1, -1, "UTF-8")) == (size_t)-1) {
  	PMNO(DOM_Exception = DOM_CHARACTER_ENC_ERR);
  	return -1;
  }
  ...
  if (utf8tods(atts[i], -1, ud) == (size_t)-1) {
  	AMSG("");
  	return;
  }
  ...
  if (DOM_DocumentLS_load(doc, argv[1]) == -1 ||
  		DOM_DocumentLS_fwrite(doc, stdout) == -1) {
  	MMSG("Failed to process %s", argv[1]);
  	return EXIT_FAILURE;
  }
  </PRE>
<BR>
</P>
<H3>10.2. Message management functions</H3>
<A name="add_codes"></A>
<P>
</P>
<B CLASS="tfc">The <TT>msgno_add_codes</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/msgno.h&gt;
  int msgno_add_codes(struct msgno_entry *list);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>mnsgo_add_codes</TT> function registers an array of <tt>msgno_entry</tt> structures. The array must contain at least one element and the <tt>msg</tt> member of the last element must be <tt>NULL</tt>. Each <tt>msgno</tt> value must be greater than the previous value. Values will be created at runtime if not provided (e.g. all 0s becomes 0,1,2,3,..). Create macros for each message value by referencing the <tt>msgno</tt> member like the following:
<PRE>

  #define DOM_INDEX_SIZE_ERR              dom_codes[0].msgno
  #define DOM_DOMSTRING_SIZE_ERR          dom_codes[1].msgno
  
  struct msgno_entry dom_codes[] = {      
      { 1, "The index specified was out of range" },
      { 0, "The text size is out of range" },
      ...
      { 0, NULL }
  };
  </PRE>
	<B>Returns</B>
<BR>
The <TT>msgno_add_codes</TT> function returns 0 if the operation was successful. Otherwise -1 is returned and <tt>errno</tt> is set appropriately.
	<P>
</P>
<A name="msg"></A>
<P>
</P>
<B CLASS="tfc">The <TT>msgno_msg</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/msgno.h&gt;
  const char *msgno_msg(unsigned int msgno);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>msgno_msg</TT> function returns the message associated with the <tt>msgno</tt> parameters that have previously been registered with <TT>msgno_add_codes</TT>. If no such message has been registered, then the message "No such msgno list" or "No such message in msgno list" is returned.
	<BR>
<P>
</P>
<A name="hdlr_stderr"></A>
<P>
</P>
<B CLASS="tfc">The <TT>msgno_hdlr_stderr</TT> function</B>
<BR>
<B>Synopsis</B>
<PRE>
<BR>  #include &lt;mba/msgno.h&gt;
  int msgno_hdlr_stderr(const char *fmt, ...);<BR>
</PRE>
<B>Description</B>
<BR>
The <TT>msgno_hdlr_stderr</TT> function writes msgno messages to <tt>stderr</tt>. It is the default msgno message handler. The msgno message handler may be changed by reassigning a new function that matches the signature to the <TT>msgno_hdlr</TT> function pointer.
<p>
</p>
Tip: If you are working on a Microsoft Windows MFC application, create a <tt>msgno_hdlr</tt> function like the one below that calls <tt>AfxMessageBox</tt> and set it to <tt>msgno_hdlr</tt> in <tt>InitInstance</tt>. This will permit your MFC application to report errors generated from within libmba.
<PRE>

  static int
  MessageBoxHdlr(const char *fmt, ...)
  {
  	char mbs[4096];
  	wchar_t wcs[4096];
  	va_list ap;
  	va_start(ap, fmt);
  
  	_vsnprintf(mbs, 4096, fmt, ap);
  	if (mbstowcs(wcs, mbs, 4096) != (size_t)-1) {
  		AfxMessageBox(wcs);
  	}
  
  	va_end(ap);
  	return 0;
  }
  BOOL CWutApp::InitInstance()
  {
  	...
  	msgno_hdlr = MessageBoxHdlr;
  </PRE>
	<B>Returns</B>
<BR>
The <TT>msgno_hdlr_stderr</TT> function (i.e. the <TT>msgno_hdlr</TT> function) returns the number of characters printed to <tt>stderr</tt>.
	<P>
</P>
<HR NOSHADE>
<small>
	Copyright 2002 Michael B. Allen &lt;mba2000 ioplex.com&gt;
</small>
</BODY>
</HTML>
